package portablejim.veinminer.asm;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;
import cpw.mods.fml.common.*;
import cpw.mods.fml.common.versioning.ArtifactVersion;
import cpw.mods.fml.common.versioning.DefaultArtifactVersion;
import cpw.mods.fml.common.versioning.InvalidVersionSpecificationException;
import cpw.mods.fml.common.versioning.VersionRange;
import portablejim.veinminer.lib.ModInfo;

import java.io.File;
import java.security.cert.Certificate;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: james
 * Date: 15/10/13
 * Time: 12:30 AM
 * To change this template use File | Settings | File Templates.
 */
public class VeinMinerCoreContainer extends DummyModContainer {
    public VeinMinerCoreContainer() {
        super(new ModMetadata());
        ModMetadata meta = getMetadata();
        meta.modId = ModInfo.COREMOD_ID;
        meta.name = ModInfo.COREMOD_NAME;
        meta.description = ModInfo.COREMOD_DESCRIPTION;
        meta.url = ModInfo.URL;
        meta.updateUrl = ModInfo.UPDATE_URL;
        meta.version = ModInfo.VERSION;
        meta.authorList = Lists.newArrayList(ModInfo.AUTHOR);
        meta.credits = ModInfo.COREMOD_CREDITS;
        meta.parent = ModInfo.MOD_ID;
        meta.requiredMods = getRequirements();
        meta.dependencies = getDependencies();
        meta.autogenerated = false;
    }

    @Override
    public String getModId() {
        return ModInfo.COREMOD_ID;
    }

    @Override
    public String getName() {
        return ModInfo.COREMOD_NAME;
    }

    @Override
    public String getVersion() {
        return ModInfo.VERSION;
    }

    @Override
    public Set<ArtifactVersion> getRequirements() {
        ArtifactVersion forge = new DefaultArtifactVersion("Forge", true);
        ArtifactVersion veinMiner = new DefaultArtifactVersion(ModInfo.MOD_ID, ModInfo.VERSION);
        return Sets.newHashSet(forge, veinMiner);
    }

    @Override
    public List<ArtifactVersion> getDependencies() {
        ArtifactVersion forge = new DefaultArtifactVersion("Forge", true);
        return Lists.newArrayList(forge);
    }

    @Override
    public String getSortingRules() {
        return ModInfo.MOD_ID;
    }

    @Override
    public boolean registerBus(EventBus bus, LoadController controller)
    {
        bus.register(this);
        return true;
    }

    @Override
    public String getDisplayVersion() {
        return ModInfo.VERSION;
    }

    @Override
    public VersionRange acceptableMinecraftVersionRange() {
        try {
            return VersionRange.createFromVersionSpec(ModInfo.VALID_MC_VERSIONS);
        } catch (InvalidVersionSpecificationException e) {
            e.printStackTrace();
        }
        return Loader.instance().getMinecraftModContainer().getStaticVersionRange();
    }

    @Override
    public Certificate getSigningCertificate() {
        try {
            Certificate[] certs = getClass().getProtectionDomain().getCodeSource().getCertificates();
            if(certs.length > 0) {
                return certs[0];
            }
        }
        catch (NullPointerException e) { }
        return null;
    }
}
